<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:OplusCms 後臺 產品單位
 *@param datetime 2012-01-12
 *@param author abin
 ********************/
class Productunits extends Admins_Controller {

  private $data=array();
  private $unique_id;
  public function __construct(){
    parent::__construct();
    $this->data['currentPage']='manages';
    $this->load->model('productunits_mdl');
    $this->data['format_time']=$this->settings['default_time'];
    //查詢語言
    $this->load->model('languages_mdl');
    $this->data['languages']=$this->languages_mdl->get_core_languages(array('status'=>1));
    $this->load->model('settings_mdl');
    $productunits=$this->settings_mdl->get_settings_by_where(array('group'=>'other','key'=>'defalut_productunits'));
    $this->data['defalut_productunits']=!empty($productunits)?$productunits['value']:'';
	$this->data['lang'] =$this->lang->language;
  }

  public function index(){
    $this->get_list();
    //$this->output->enable_profiler(true);
    $this->load->view('admins/productunits_list_view',$this->data);
  }

  //獲取列表
  private function get_list(){
    $page = $this->input->get('p',TRUE);
    $page = (!empty($page) && is_numeric($page)) ? intval($page) : 1;
    $limit = $this->settings['default_admin_limit'];
    //$limit =1;
    $offset = ($page - 1) * $limit;
    if($offset < 0){
      redirect('admins/productunits');
    }
    $url_query=$_SERVER['QUERY_STRING'];
    // 保留查詢參數;
    $cookie = array(
        'name'   => 'url_query',
        'value'  => base64_encode($url_query),
        'expire' => '0',
        'path'   => '/',
    );
    $this->input->set_cookie($cookie);

    if(!empty($url_query)){
      $url_query=preg_replace('/&p=(\d+)/','',$url_query);
    }
    $this->data['name']=$this->input->get('name');

    $this->data['field']=$this->input->get('field');
    $this->data['sort']=$this->input->get('sort');
    $this->data['field']=!empty($this->data['field'])?$this->data['field']:'name';
    $this->data['sort']=!empty($this->data['sort'])?$this->data['sort']:'desc';

    $data=array('language_id'=>$this->settings['default_language'],'limits'=>$limit,'offset'=>$offset);
    $data=!empty($this->data['name'])?array_merge($data,array('name'=>trim($this->data['name']))):$data;

    $data=!empty($this->data['field'])?array_merge($data,array('field'=>$this->data['field'])):$data;
    $data=!empty($this->data['sort'])?array_merge($data,array('sort'=>strtoupper($this->data['sort']))):$data;

    $this->data['lists_count']=$this->productunits_mdl->totle_productunits($data);

    $pagination = '';
    if($this->data['lists_count'] > $limit)
    {
      $this->dpagination->currentPage($page);
      $this->dpagination->items($this->data['lists_count']);
      $this->dpagination->limit($limit);
      $this->dpagination->adjacents(5);
      $this->dpagination->target(site_url('admins/productunits').'?'.$url_query);
      $this->dpagination->parameterName('p');
      $this->dpagination->nextLabel($this->data['lang']['orders_lable_19']);
      $this->dpagination->PrevLabel($this->data['lang']['orders_lable_18']);
      $pagination = $this->dpagination->getOutput();
    }
    $this->data['pagination'] = $pagination;
    $this->data['lists']=$this->productunits_mdl->get_productunits($data);
  }

  //添加按鈕頁面跳轉
  public function add(){
     $this->load->view('admins/productunits_form_view',$this->data);
  }

  //修改按鈕頁面跳轉
  public function edit($edit_id=0){

    if(!empty($edit_id) && intval($edit_id)>0){

      $this->data['edit_id']=$edit_id;
      //productunits
      $results=$this->productunits_mdl->get_descriptions($edit_id);

      foreach($results->result_array() as $rs){
        $this->data['descriptions'][$rs['language_id']]=$rs;
      } //end foreach;
      
      $this->load->view('admins/productunits_form_view',$this->data);
    }else{
      show_error('對不起,參數出錯!');
      exit;
    }
  }

  //執行修改或添加功能
  public function modify(){
    $edit_id=$this->input->post('edit_id');
    $this->unique_id=$edit_id;
    $this->_load_validation_rules();
    if ($this->form_validation->run() == FALSE){
      $this->data['error']=true;
      $this->load->view('admins/productunits_form_view',$this->data);
    }else{
      //多語言描述
      $data['descriptions']=$this->input->post('descriptions');
      if(!empty($edit_id)){
        $operator_name='修改產品單位->'.$data['descriptions'][$this->settings['default_language']]['name'];
        $action='修改';
        $result=$this->productunits_mdl->edit_productunits($data,$edit_id);
      }else{
        $operator_name='新增產品單位->'.$data['descriptions'][$this->settings['default_language']]['name'];
        $action='新增';
        $result=$this->productunits_mdl->add_productunits($data);
      }
      $this->operator_log($operator_name,$action,$result);
      $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
      $notify = $result > 0?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      $redirect=site_url('admins/productunits').$this->settings['url_query'];//獲取列表篩選參數
      redirect($redirect);
    }
  }

  //執行刪除功能
  public function delete($delete_id=0){
    if(!empty($delete_id)){
      $result=$this->productunits_mdl->delete_productunits($delete_id);
      $this->operator_log('刪除產品單位->ID:'.$delete_id,'刪除',$result);
      $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
      $notify = $result > 0?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      $redirect=site_url('admins/productunits').$this->settings['url_query'];//獲取列表篩選參數
      redirect($redirect);
    }else{
      show_error('對不起,參數出錯!');
      exit;
    }
  }
   //批量刪除
  public function delete_batch(){
     $delete_string=$this->input->get('delete_string');
     if(!empty($delete_string)){
        $del_arr=explode(',',$delete_string);
        $result=$this->productunits_mdl->delete_batch($del_arr);

        $this->operator_log('批量刪除產品單位->ID:'.$delete_string,'批量刪除',$result);

        $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
        $notify = $result > 0?'success':'error';
        $this->nsession->set_flashdata($notify, $msg);
        $redirect=site_url('admins/productunits').$this->settings['url_query'];//獲取列表篩選參數
        redirect($redirect);
      }else{
        show_error('對不起,參數出錯');
        exit;
      }
   }
  //驗證數據格式
  private function _load_validation_rules(){
    $this->form_validation->set_rules('edit_id','修改ID','trim|numeric');
    //多語言判斷
    foreach($this->data['languages']->result_array() as $lg){
      $name='descriptions['.$lg["id"].'][name]';
      $this->form_validation->set_rules($name,$lg['name'].' 狀態','trim|required|max_length[150]');
    }
  }
}